package com.fs.warehouse.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.page.PageDomain;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.core.pojo.SysUser;
import com.fs.common.core.vo.AjaxResult;
import com.fs.common.util.page.PageUtils;
import com.fs.procure.pojo.Insto;
import com.fs.procure.vo.InstoVo;
import com.fs.system.service.ISysUserService;
import com.fs.system.util.SecurityUtils;
import com.fs.warehouse.mapper.WhInvInventoryMapper;
import com.fs.warehouse.pojo.*;
import com.fs.warehouse.service.WarehouseService;
import com.fs.warehouse.vo.DefectiveProductVo;
import com.fs.warehouse.vo.InventoryVo;
import com.fs.warehouse.vo.WarehousingVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/warehouse")
public class WarehouseController extends BaseController {

    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 获取所有仓库
     * @return 统一响应格式
     */
    @GetMapping("getWarehouse")
    public AjaxResult getWarehouse(){
        AjaxResult ajaxResult = AjaxResult.success();
        List<WhWarehouse> warehouses = warehouseService.getAllWarehouse();
        ajaxResult.put("warehouses",warehouses);
        ajaxResult.put("msg","获取成功");
        return ajaxResult;
    }
    /**
     * 获取该部门所有普通员工
     * @return 统一响应格式
     */
    @GetMapping("getOrdinaryEmployees")
    public AjaxResult getOrdinaryEmployees(){
        AjaxResult ajaxResult = AjaxResult.success();
        Long deptId = SecurityUtils.getDeptId();
        List<SysUser> allOrdinaryEmployees = sysUserService.getAllOrdinaryEmployeesByDeptId(deptId.intValue());
        ajaxResult.put("allOrdinaryEmployees",allOrdinaryEmployees);
        return ajaxResult;
    }
    /**
     * 发起库存盘点
     * @return 统一响应格式
     */
    @PostMapping("initiateInventory")
    public AjaxResult initiateInventory(@RequestBody InventoryVo inventoryVo){
        AjaxResult ajaxResult = AjaxResult.success();
        warehouseService.initiateInventory(inventoryVo.getWarehouseId(),inventoryVo.getInventoryTaKerId(),inventoryVo.getInventoryTime(),inventoryVo.getProducts());
        return ajaxResult;
    }

    /**
     * 获取所有库存盘点
     * @param inventoryVo 库存盘点对象
     * @return 表格数据
     */
    @PostMapping("getAllInventory")
    public TableDataInfo getAllInventory(@RequestBody InventoryVo inventoryVo){
        PageDomain pageDomain = PageUtils.startPage();
        if(Objects.nonNull(inventoryVo.getCurrentPage()))
            pageDomain.setPageNum(inventoryVo.getCurrentPage());
        if(Objects.nonNull(inventoryVo.getPageSize()))
            pageDomain.setPageSize(inventoryVo.getPageSize());
        Page<WhInvInventory> page = warehouseService.selectInventory(pageDomain,inventoryVo);
        return this.getDataTable(page);
    }

    /**
     * 获取所有入库申请接口
     * @param instoVo 入库申请Vo对象
     * @return 表格数据
     */
    @GetMapping("getWarehousingApplication")
    public TableDataInfo getWarehousingApplication(@RequestBody InstoVo instoVo){
        PageDomain pageDomain = PageUtils.startPage();
        Page<Insto> page = warehouseService.selectWarehousingApplication(pageDomain,instoVo);
        return this.getDataTable(page);
    }

    /**
     * 指定入库员工
     * @param inStoId 入库申请编号
     * @param userName 入库员工姓名
     * @return 统一响应格式
     */
    @PostMapping("designatedEmployeeInv")
    public AjaxResult designatedEmployeeInv(@RequestBody String inStoId,String userName){
        AjaxResult ajaxResult = AjaxResult.success();
        warehouseService.designatedEmployeeInv(inStoId,userName);
        return ajaxResult;
    }


    /**
     * 生成入库单
     * @param warehousingVo 入库单的vo对象
     * @return 统一响应格式
     */
    @PostMapping("generateWarehousing")
    public AjaxResult generateWarehousing(@RequestBody WarehousingVo warehousingVo){
        AjaxResult ajaxResult = AjaxResult.success();
        warehouseService.generateWarehousing(warehousingVo);
        return ajaxResult;
    }

    /**
     * 获取所有入库单
     * @param warehousingVo 入库单条件
     * @return 表格信息数据
     */
    @GetMapping("getAllWarehousing")
    public TableDataInfo getAllWarehousing(@RequestBody WarehousingVo warehousingVo){
        PageDomain pageDomain = PageUtils.startPage();
        Page<WhWarehousing> page = warehouseService.selectWarehousingList(pageDomain, warehousingVo);
        return  this.getDataTable(page);
    }

    @PostMapping("updateStatus")
    public AjaxResult updateStatus(@RequestBody WhInvInventory invInventory){
        AjaxResult ajaxResult = AjaxResult.success();
        WhInvInventory whInvInventory = warehouseService.updateStatus(invInventory);
        ajaxResult.put("invInventory",whInvInventory);
        return ajaxResult;
    }

    @PostMapping("fillInventoryDetail")
    public AjaxResult fillInventoryDetail(@RequestBody List<WhInvInventoryDetail> invInventoryDetails){
        AjaxResult ajaxResult = AjaxResult.success();
        WhInvInventory newInvInventory = warehouseService.fillInventoryDetail(invInventoryDetails);
        ajaxResult.put("newInvInventory",newInvInventory);
        return ajaxResult;
    }
    @PostMapping("defectiveApplication")
    public AjaxResult defectiveApplication(@RequestBody List<WhDefectiveHandleApplication> defectiveHandle){
        AjaxResult ajaxResult = AjaxResult.success();
        warehouseService.submitDefectiveApplication(defectiveHandle);
        return ajaxResult;
    }
    @PostMapping("selectDefectiveApplication")
    public TableDataInfo selectDefectiveApplication(@RequestBody DefectiveProductVo defectiveProductVo){
        PageDomain pageDomain = PageUtils.startPage();
        Page<DefectiveProductVo> page  = warehouseService.selectDefectiveApplication(pageDomain,defectiveProductVo);
        return this.getDataTable(page);
    }

    @PostMapping("updateDefectiveStatus")
    public AjaxResult updateDefectiveStatus(@RequestBody DefectiveProductVo defectiveProductVo){
        AjaxResult ajaxResult = AjaxResult.success();
        warehouseService.updateDefectiveStatus(defectiveProductVo);
        return ajaxResult;
    }

    @PostMapping("generateOutbound")
    public AjaxResult generateOutbound(@RequestBody WhOutbound outbound){
        AjaxResult ajaxResult = AjaxResult.success();
        warehouseService.generateOutbound(outbound);
        return ajaxResult;
    }
    @Autowired
    private WhInvInventoryMapper invInventoryMapper;
    @GetMapping("test")
    public void test(){
        List<WhInvInventoryDetail> whInvInventoryDetail = invInventoryMapper.selectWhInvInventoryDetailByInventoryId(1);
        log.info("-----------{}",whInvInventoryDetail);
    }

}
